/**
 * jims
 */
package com.jims.master.modules.drug.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.drug.entity.DrugProvideApplication;
import com.jims.master.modules.drug.service.DrugProvideApplicationService;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 药品发放生申请Controller
 * @author zhuq
 * @version 2017-03-18
 */
@Controller
@RequestMapping(value = "${adminPath}/drug/drugProvideApplication")
public class DrugProvideApplicationController extends BaseController {

	@Autowired
	private DrugProvideApplicationService drugProvideApplicationService;

    /**
     * 查询申请出库数据
     * @param drugProvideApplication
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"query", ""})
    public String getApplicationList(DrugProvideApplication drugProvideApplication, HttpServletRequest request, HttpServletResponse response, Model model) {
        drugProvideApplication.setStorageOut(UserUtils.getUser().getOrgDept().getId());
        drugProvideApplication.updateOrgId();
        if(null == drugProvideApplication.getStorageIn() || drugProvideApplication.getStorageIn().equals("")){
            model.addAttribute("list",null);
        }else{
            List<DrugProvideApplication> list = drugProvideApplicationService.findApplicationExportList(drugProvideApplication);
            if(null != list){
                model.addAttribute("list", list);
            }else{
                model.addAttribute("list",null);
            }
        }
        return "modules/drug/drugApplicationExport";
    }

    /**
     * 申请出库数据保存
     * @param drugProvideApplication
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"saveProvideApplication",""})
    @ResponseBody
    public StringData saveProvideApplication(DrugProvideApplication drugProvideApplication){
        StringData data = new StringData();
        int num = drugProvideApplicationService.saveProvideApplication(drugProvideApplication);
        if(num > 0){
            data.setCode("success");
            data.setData("申请出库成功!");
        }else{
            data.setCode("error");
            data.setData("申请出库失败!");
        }
        return data;
    }
	
	@ModelAttribute
	public DrugProvideApplication get(@RequestParam(required=false) String id) {
		DrugProvideApplication entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = drugProvideApplicationService.get(id);
		}
		if (entity == null){
			entity = new DrugProvideApplication();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(DrugProvideApplication drugProvideApplication, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DrugProvideApplication> page = drugProvideApplicationService.findPage(new Page<DrugProvideApplication>(request, response), drugProvideApplication); 
		return "modules/drug/drugProvideApplicationIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(DrugProvideApplication drugProvideApplication, Model model,HttpServletRequest request,HttpServletResponse response) {
		model.addAttribute("drugProvideApplication", drugProvideApplication);
		return "modules/drug/drugProvideApplicationForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(DrugProvideApplication drugProvideApplication, Model model) {
	    StringData data=new StringData();
		int i=drugProvideApplicationService.save(drugProvideApplication);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(DrugProvideApplication drugProvideApplication) {
	    StringData data=new StringData();
		int i=drugProvideApplicationService.delete(drugProvideApplication);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}

}